package com.bingo.food.orderservicemanager.moodymq.config;

import com.bingo.food.orderservicemanager.moodymq.listener.DlxListener;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnProperty(name = "moodymq.dlxEnabled", havingValue = "true")
public class DlxConfig {
    @Bean
    public TopicExchange dlxExchange(){
        return new TopicExchange("exchange.dlx");
    }

    @Bean
    public Queue dlxQueue(){
        return new Queue("queue.dlx",
                true,
                false,
                false);
    }

    @Bean
    public Binding dlxBinding(){
        return BindingBuilder.bind(dlxQueue()).to(dlxExchange()).with("#");
    }

    @Bean
    public SimpleMessageListenerContainer deadLetterListenerContainer(ConnectionFactory connectionFactory, DlxListener dlxListener) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
        container.setQueues(new Queue[]{this.dlxQueue()});
        container.setExposeListenerChannel(true);
        container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        container.setMessageListener(dlxListener);
        container.setPrefetchCount(100);
        return container;
    }
}
